package com.imax.emap.overlay
{
	import com.imax.emap.mapProvider.IMapProvider;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.*;
	
	import mx.core.IDataRenderer;
	import mx.core.UIComponent;
	import mx.effects.AnimateProperty;
	
	public class MapTile extends UIComponent implements IDataRenderer
	{
		private var _publicAlpha:Number = 1;
		private var _fadeValue:Number = 1;
		private var _data:Object;
		private var _inUse:Boolean;

		private var _url:String;
		private var _loader:Loader;
		
		private var _mapProvider:IMapProvider;
		
		public function MapTile(){
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.INIT,loadComplete);
					
			visible = false;
		}
		
		public function cancleLoad():void{
			//_loader.unload();
			_loader.close();
		}	
		
		public function set provider(tc:IMapProvider):void{
			this._mapProvider = tc;
			
			if(_data){
				this.loadTile();
			}
		}
		public function get provider():IMapProvider{
			return this._mapProvider;
		}
		
		
		private function loadComplete(e:Event):void
		{
			this.addChild(_loader);
			visible = true;
			//AnimateProperty的效率要高于Fade
			var f:AnimateProperty = new AnimateProperty(this);
			f.property = "fadeValue";
			f.toValue= 1;
			f.fromValue = 0;
			f.play();
		}
		
		private function loadTile():void{
			_url = this._mapProvider.getTileURL(_data.x, _data.y, _data.z);
			trace("瓦片地址:"+_url);
			_loader.load(new URLRequest(_url));
			
		}
		
		public function set data(value:Object):void
		{
			_data = value;
			if(this._mapProvider){
				this.loadTile();
			}

		}
		
		public function get data():Object { return _data;}
		
		public function set fadeValue(value:Number):void
		{
			_fadeValue = value;
			super.alpha = _publicAlpha*_fadeValue;
		}
		
		override public function get id():String{
			return this._data.x + "_" + this._data.y + "_" + this._data.z;
		}

		public function get inUse():Boolean{
			return this._inUse;
		}
		
		public function set inUse(u:Boolean):void{
			this._inUse = u;
		}

	}
}